/*============================================================================*/
/*                              x embedded systems                            */
/*============================================================================*/
/*                        OBJECT SPECIFICATION                                */
/*============================================================================*
* %name:            hw_timers.h%
* %version:         1.0%
* %created_by:      David Robles%
* %date_created:    March 14 2012%
*=============================================================================*/
/* DESCRIPTION : Timers handling                                              */
/*============================================================================*/
/* FUNCTION COMMENT : Provides APIs to configure and use Timers of PIC micro  */
/*                                                                            */
/*                                                                            */
/*============================================================================*/
/*                               OBJECT HISTORY                               */
/*============================================================================*/
/*  REVISION |   DATE      |                               |      AUTHOR      */
/*----------------------------------------------------------------------------*/
/*  1.0      | 02/14/2012  |                               | David Robles     */
/* File created                                                               */
/*============================================================================*/

#ifndef HW_TIMERS_H
#define HW_TIMERS_H

/* Includes */
/* -------- */
#include "stdtypedef.h"

/*======================================================*/ 
/* Definition of RAM variables                          */
/*======================================================*/ 
/* BYTES */

/* WORDS */
extern T_UWORD ruw_TimerCounter;

/* LONGS and STRUCTURES */

/* Exported functions prototypes and macros */
/* ---------------------------------------- */

/* Functions prototypes */
PUBLIC_FCT void timer0_initCfg(void);
PUBLIC_FCT T_UBYTE timer0_finished(void);
PUBLIC_FCT void timer0_Interrupt_Handler(void);

/* Functions macros */
// Use these macro to set timer0 in 16 bits mode
#define	Set_Timer0m16b(value)			TMR0H=((T_UBYTE)((value)>>8));TMR0L=((T_UBYTE)((value)&0x00FF))
#define	Set_Timer0_16b(H_byte,L_byte)	TMR0H=(H_byte);TMR0L=(L_byte) 
// Use these macros to read the timer0 in 16 bits mode
#define	Get_Timer0m16b()				(((T_UWORD)(TMR0L&0x00FF))|(((T_UWORD)TMR0H)<<8))
#define	Get_Timer0_Hbyte()				(TMR0H)
#define	Get_Timer0_Lbyte()				(TMR0L)

/* Exported defines */
/*
r1 = (8000000/4) = 2,000,000	% Frecuency of timer 0 clock
r2 = (2^16)-1 = 65,535 			% Maximun counts of timer 0
r3 = 1/r1 = 0.0000005 			% Time value by increment of timer 0 or resolution
r4 = r2*r3 = 0.0327675 			% Maximun time value possible to reach in current configuration of timer 0
r5 = 0.001/r3 = 2,000 			% Number of counts in timer 0 to generate 1 ms time
r6 = r2-r5 = 63,535 			% Pre-load value in timer 0 to generate a 1 ms delay/interrupt
r7 = hex(r6) = 0xF82F			% Value in hex of pre-load timer 0 value
*/
#define _TMR0L_INITVALUE_	0x2F
#define _TMR0H_INITVALUE_	0xF8
#define _TMR0_INITVALUE_	63535

#define Get_BaseTime() ruw_TimerCounter


#endif